﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyCongViec.View;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using QuanLyCongViec.Common;
using QuanLyCongViec.Database;
namespace QuanLyCongViec.View
{
    public partial class FrmHopDong : Form
    {
        Database.dbHopDong db = new QuanLyCongViec.Database.dbHopDong();
        private const string CAPTION_NGAYGIAO = "Ngày giao hàng";
        private const string CAPTION_NGAYHOPDONG = "Ngày hợp đồng";
        private const string CAPTION_MALOAIHOPDONG = "Mã loại hợp đồng";
        private const string CAPTION_MAHOPDONG = "Mã hợp đồng";
        private const string CAPTION_GROUP_HEADER = "Thông tin tìm kiếm ";
        private const string CAPTION_GROUP_GRID = "Thông tin hợp đồng";
        private const string CAPTION_FORM = "Quản lý hợp đồng";

        public FrmHopDong()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private void InitControl()
        {
            lbNgayGiao.Text = CAPTION_NGAYGIAO;
            lbNgayHD.Text = CAPTION_NGAYHOPDONG;
            lbMaLoaiHD.Text = CAPTION_MALOAIHOPDONG;
            lbMaHD.Text = CAPTION_MAHOPDONG;
            gbHeader.Text = CAPTION_GROUP_HEADER;
            gbGrid.Text = CAPTION_GROUP_GRID;
            this.Text = CAPTION_FORM;
            btnCreate.Text = BtnControl.CAPTION_BTN_CREATE;
            btnSave.Text = BtnControl.CAPTION_BTN_SAVE;
            btnSearch.Text = BtnControl.CAPTION_BTN_SEARCH;
        }
        private void InitGrid()
        {

            DataTable dt = db.HopDongGet("", "","");
            MaLoaiHopDong.Caption = CAPTION_MALOAIHOPDONG;
            MaHopDong.Caption = CAPTION_MAHOPDONG;
            NgayGiaoHang.Caption = CAPTION_NGAYGIAO;
            NgayHopDong.Caption = CAPTION_NGAYHOPDONG;

            MaLoaiHopDong.FieldName = TblHopDong.MaLoaiHD;
            MaHopDong.FieldName = TblHopDong.MaHD;
            NgayHopDong.FieldName = TblHopDong.NgayHD;
            NgayGiaoHang.FieldName = TblHopDong.NgayGiao;
            HopDongID.FieldName = TblHopDong.HopDongID;
            gridHopDong.DataSource = dt;
            gviewHopDong.OptionsBehavior.Editable = false;
        }
        private void Search()
        {
            DataTable dt = db.HopDongGet("",txtMaHD.Text,txtMaLoaiHD.Text);
            gridHopDong.DataSource = dt;

        }
        private void Save()
        {
            db.HopDongCreate(dedNgayGiao.Text, dedNgayHD.Text, txtMaLoaiHD.Text, txtMaHD.Text);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            InitGrid();
            InitControl();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            lbNgayGiao.Text = now.Hour + ":" + now.Minute + ":" + now.Second;
            DateTime hen = new DateTime(2011,04,24,12,05,00);
            if(now == hen)
            {
            MessageBox.Show("den gio");
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
        
        }

        private void txtLoaiHopDong_TextChanged(object sender, EventArgs e)
        {
        
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            Save();
        }

        private void btnSearch_Click_1(object sender, EventArgs e)
        {
            Search();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {

            FrmHopDongCreate frm = new FrmHopDongCreate();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();
        }

        private void gviewHopDong_DoubleClick(object sender, EventArgs e)
        {
            MouseEventArgs me = e as MouseEventArgs;
            MouseButtons btn = me.Button;
            int x = me.X;
            int y = me.Y;
            GridHitInfo hit = gviewHopDong.CalcHitInfo(x, y);
            if (hit.InRowCell)
            {
                FrmHopDongModify frm = new FrmHopDongModify();
                DataRow dr = gviewHopDong.GetDataRow(gviewHopDong.FocusedRowHandle);
                frm.StartPosition = FormStartPosition.CenterScreen;
                frm.HopDongID = dr[TblHopDong.HopDongID].ToString();
                frm.ShowDialog();
            }
        }

      
    }
}
